package com.java.util;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 *
 */
public class JSON {
    private final static Logger logger = LoggerFactory.getLogger(JSON.class);
    public static ObjectMapper mapper = new ObjectMapper();
    public static ObjectMapper prettyMapper = new ObjectMapper();

    public JSON() {
    }

    public static String encode(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception var2) {
            logger.error("Failed to encode as JSON:{}", var2.getMessage());
            return null;
//            throw new EncodeException("Failed to encode as JSON: " + var2.getMessage());
        }
    }

    public static String signEncode(Object obj, String key) {
        return signEncode(obj, key, "appKey");
    }

    public static String signEncode(Object obj, String key, String signName) {
        try {
            String json = null;
            if (obj instanceof String)
                json = (String) obj;
            else
                json = encode(obj);
            Map<String, String> map = decodeMap(json, String.class, String.class);
            if (StringUtils.isNotBlank(signName))
                map.remove(signName);
            else
                return null;
            String url = SignUtils.payParamsToString(map, true);
            String sign = MD5Util.getSign(url, key);
            map.put(signName, sign);
            return encode(map);
        } catch (Exception e) {
            logger.error("Failed to signEncode as JSON:{}", e.getMessage());
            return null;
        }
    }

    public static String encodePrettily(Object obj){
        try {
            return prettyMapper.writeValueAsString(obj);
        } catch (Exception var2) {
            logger.error("Failed to encode as JSON:{}", var2.getMessage());
            return null;
//            throw new EncodeException("Failed to encode as JSON: " + var2.getMessage());
        }
    }

    public static <T> T decodeValue(String str, JavaType javaType){
        try {
            return mapper.readValue(str, javaType);
        } catch (Exception var3) {
            logger.error("Failed to decode:{}", var3.getMessage());
            return null;
//            throw new DecodeException("Failed to decode:" + var3.getMessage());
        }
    }

    public static <T> T decodeValue(String str, Class<T> clazz){
        try {
            return mapper.readValue(str, clazz);
        } catch (Exception var3) {
            logger.error("Failed to decode:{}", var3.getMessage());
            return null;
//            throw new DecodeException("Failed to decode:" + var3.getMessage());
        }
    }

    public static <T> List<T> decodeArray(String str, Class<T> clazz){
        try {
            return mapper.readValue(str, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
        } catch (Exception var3) {
            logger.error("Failed to decode:{}", var3.getMessage());
            return null;
//            throw new DecodeException("Failed to decode:" + var3.getMessage());
        }
    }

    public static <K, V> Map<K, V> decodeMap(String str, Class<K> keyClass, Class<V> valueClass){
        try {
            return mapper.readValue(str, mapper.getTypeFactory().constructMapType(Map.class, keyClass, valueClass));
        } catch (Exception var3) {
            logger.error("Failed to decode:{}", var3.getMessage());
            return null;
//            throw new DecodeException("Failed to decode:" + var3.getMessage());
        }
    }

}
